import { _decorator, Button, Component, Node } from 'cc';
import { LayerBase } from './LayerBase';
import { UserInfo } from './UserInfo';
import { rewardEnum } from '../Enum';
import { UILayerMgr } from './UILayerMgr';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { randomNum } from '../utils/utils';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('EveryDaySignPanel')
export class EveryDaySignPanel extends LayerBase {

    @property(Node)
    moduleRoot:Node

    init(somedData?: any): void {
        for (let i = 0; i < this.moduleRoot.children.length; i++) {
            this.moduleRoot.children[i].getChildByName("isAlreadySign").active=false;
            this.moduleRoot.children[i].getComponent(Button).enabled=false;
        }

        console.log(UserInfo.signContinuousDayNum,"UserInfo.signContinuousDayNum")
        for (let t = 0; t < UserInfo.signContinuousDayNum+1; t++) {
            console.log("显示已签到")
            this.moduleRoot.children[t].getChildByName("isAlreadySign").active=true;
        }

        if (!UserInfo.todayAlreadyIsSign) {
            if (UserInfo.signContinuousDayNum>5) {
                this.moduleRoot.children[6].getComponent(Button).enabled = true;
            }
            else{
                this.moduleRoot.children[UserInfo.signContinuousDayNum + 1].getComponent(Button).enabled = true;
            }
        }
    }

    onclickGetSignRewardBtn(event, index) {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        this.moduleRoot.children[index].getComponent(Button).enabled=false;
        switch (index) {
            case "0":
                this.getCrystal(50);
                break;
            case "1":
                this.getEpicBoxKey(1);
                break;
            case "2":
                this.getRandomEndSkillProp(1);
                break;
            case "3":
                this.getCrystal(130);
                break;
            case "4":
                this.getCoin(150);
                break;
            case "5":
                this.getRandomEndSkillProp(3);
                break;
            case "6":
                StartSceneGameMgr.ins.getRole(4);
                let rewardCongfi = UserInfo.roleCongfiDic.get(4);
                rewardCongfi.count = 1;
                UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
                break;
        }
        UserInfo.todayAlreadyIsSign=true;
        UserInfo.signContinuousDayNum+=1;
        console.log("点击签到UserInfo.signContinuousDayNum",UserInfo.signContinuousDayNum);
        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI(this.node.name);
    }

    getCrystal(Num) {
        UserInfo.crystalMun+=Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.crystal);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    getCoin(Num) {
        UserInfo.coinMun+=Num;
        let rewardCongfi=UserInfo.rewardCongfiDic.get(rewardEnum.coin);
        rewardCongfi.count=Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    //得到史诗宝箱钥匙
    getEpicBoxKey(Num) {
        UserInfo.epicBoxKeyNum += Num;
        let rewardCongfi = UserInfo.rewardCongfiDic.get(rewardEnum.epicBoxKey);
        rewardCongfi.count = Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);
    }

    //获得随机道具卡
    getRandomEndSkillProp(Num) {
        let randCount = randomNum(1, 100);
        let getTypeEndSkill = 0;

        if (0 < randCount && randCount < 40) {//40%
            getTypeEndSkill = 0
        }
        else if (40 <= randCount && randCount < 70) {//30%
            getTypeEndSkill = 1
        }
        else if (70 <= randCount && randCount <= 100) {//30%
            getTypeEndSkill = 2
        }

        UserInfo.hasEndSkillPropArr[getTypeEndSkill] += Num;

        let rewardCongfi = UserInfo.rewardCongfiDic.get(getTypeEndSkill + 3);
        rewardCongfi.count = Num;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);

    }

}


